<?php

/**
 * This is the model class for table "{{users_messages}}".
 *
 * The followings are the available columns in table '{{users_messages}}':
 * @property integer $id
 * @property string $text
 * @property integer $from_id
 * @property integer $to_id
 * @property integer $remove_from
 * @property integer $remove_to
 * @property string $crdate
 * @property integer $read
 */
class UsersMessages extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UsersMessages the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{users_messages}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('text', 'required'),
			array('from_id, to_id, remove_from, remove_to, read', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, text, from_id, to_id, remove_from, remove_to, crdate, read', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user_from' => array(self::BELONGS_TO, 'Users', 'from_id',
				'select'=>'id,avatar,full_name'
			),
			'user_to' => array(self::BELONGS_TO, 'Users', 'to_id',
				'select'=>'id,avatar,full_name'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'text' => 'Text',
			'from_id' => 'From',
			'to_id' => 'To',
			'remove_from' => 'Remove From',
			'remove_to' => 'Remove To',
			'crdate' => 'Crdate',
			'read' => 'Read',
		);
	}

	public function getDialogs(){
		$connection=Yii::app()->db;
		$sql='SELECT t.id,
					 t.text, 
					 t.crdate, 
					 t.read,
					 t.from_id,
					 IF(`from_id` = ' . (int)Yii::app()->user->id . ', `to_id`, `from_id`) AS dialog, 
					 u.id as user_id,
					 u.full_name,
					 u.avatar as avatar
			    FROM (SELECT * FROM {{users_messages}} WHERE `from_id`=' . (int)Yii::app()->user->id . ' OR `to_id`=' . (int)Yii::app()->user->id . ' ORDER BY crdate DESC) AS t 
		   LEFT JOIN {{users}} as u
		   		  ON IF(t.`from_id` = ' . (int)Yii::app()->user->id . ', t.`to_id`, t.`from_id`) = u.id
			GROUP BY dialog  
			ORDER BY t.crdate DESC';
		
		return $connection->createCommand($sql)->queryAll();	
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('text',$this->text,true);
		$criteria->compare('from_id',$this->from_id);
		$criteria->compare('to_id',$this->to_id);
		$criteria->compare('remove_from',$this->remove_from);
		$criteria->compare('remove_to',$this->remove_to);
		$criteria->compare('crdate',$this->crdate,true);
		$criteria->compare('read',$this->read);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}